<template>
  <div class="container flow-column h100">
    <div class="container-main h100">
      <list-table-new v-bind="tableSetting" ref="tableRef"></list-table-new>
    </div>
  </div>
</template>
<script>
import { getListAndPaging } from "@/api/twoTicketManagement/twoTicketCheck.js";
export default {
  data() {
    return {
      tableSetting: {
        singleRow: true,
        showCustomBtn: true, //显示自定义列
        showFilterBtn: true, //显示过滤器
        pageConfig: {
          pageCode: "workTicketCheck", //页面标识，用来存储过滤器以及自定义列信息
          userId: this.$store.getters.userInfo.id,
        },
        // 搜索框
        searchObj: {
          searchMethod: (params, sortStr) => {
            this.getTableData(params, sortStr);
          },
          queryData: {
            page: 1,
            pageSize: 15,
          },
          searchArr: [
            {
              type: "select",
              label: "工作票类型",
              model: "type",
              option: this.$store.getters["dirtData"]("ticket_work_type"),
            },
            {
              type: "organization",
              label: "开票单位",
              model: "unitName",
              idsStr: "unit",
            },
            {
              type: "input",
              label: "标题",
              model: "title",
              placeholder: "请输入",
            },
            {
              type: "select",
              label: "抽查结果",
              model: "checkResults",
              option: this.$store.getters["dirtData"]("spot_check_results"),
            },
            {
              type: "input",
              label: "工作票票号",
              model: "code",
              placeholder: "请输入",
            },
            {
              type: "select",
              label: "是否外单位",
              model: "externalFlag",
              option: [
                { label: "是", value: 1 },
                { label: "否", value: 0 },
              ],
            },
            {
              type: "organization",
              label: "工作班组",
              model: "team",
              placeholder: "请选择或输入",
            },
            {
              type: "userOnly",
              label: "工作负责人",
              model: "directorName",
              placeholder: "请选择或输入",
            },
            {
              type: "datetimerange",
              label: "填票时间",
              model: "createTime",
            },
            {
              type: "location",
              label: "工作地点",
              model: "locationName",
              idsStr: "location",
            },
            {
              type: "datetimerange",
              label: "计划起止时间",
              model: "estimatedTime",
            },
          ],
        },
        // 操作按钮
        actionList: [],
        // 过滤器的值
        filterFormData: {},
        // 过滤器的默认值
        defaultFilterData: {},
        userCustomData: [],
        // 表头
        column: [
          {
            label: "状态",
            prop: "status",
            sortable: true,
            formatter: (row) => {
              let types = this.$store.getters["dirtData"]("ticket_work_status");
              let val = row.status;
              let obj = types.find((i) => i.value == val);
              return obj ? obj.label : val;
            },
            width: 110,
          },
          {
            label: "工作票票号",
            prop: "code",
            sortable: true,
            width: 140,
          },
          {
            label: "标题",
            prop: "title",
            sortable: true,
            width: 280,
          },
          {
            label: "工作票类型",
            prop: "type",
            sortable: true,
            formatter: (row) => {
              let types = this.$store.getters["dirtData"]("ticket_work_type");
              let val = row.type;
              let obj = types.find((i) => i.value == val);
              return obj ? obj.label : val;
            },
            width:160
          },
          {
            label: "开票单位",
            prop: "unitName",
            sortable: true,
            showTooltip:true,
            minWidth:120
          },
          {
            label: "工作负责人",
            prop: "directorName",
            sortable: true,
            minWidth:120
          },
          {
            label: "工作班组",
            prop: "team",
            sortable: true,
            showTooltip:true,
            minWidth:120
          },
          {
            label: "工作地点",
            prop: "locationName",
            sortable: true,
            showTooltip:true,
            minWidth:120
          },
          {
            label: "计划开始时间",
            prop: "estimatedStartTime",
            sortable: true,
            minWidth:140
          },
          {
            label: "计划结束时间",
            prop: "estimatedEndTime",
            sortable: true,
            minWidth:140
          },
          {
            label: "抽查结果",
            prop: "checkResults",
            sortable: true,
            scopeType: "formate",
            method: (row) => {
              if (row.checkResults) {
                return `<span>合格</span>`;
              } else if (row.checkResults == false) {
                return `<span style="color: #f42c2c">不合格</span>`;
              } else {
                return `<span>未抽查</span>`;
              }
            },
            minWidth:120
          },
        ],
        // 表格方法
        tableMethods: {
          //点击行的方法
          rowClick: (val) => {
            this.getDetails(val.id, "check", val.type);
          },
        },
      },
    };
  },
  created() {
    // let query = {
    //   unit:
    //     this.$store.state.user.userInfo.orgId ||
    //     this.$store.state.user.userInfo.deptId,
    //   unitName:
    //     this.$store.state.user.userInfo.orgName ||
    //     this.$store.state.user.userInfo.deptName,
    // };
    // this.tableSetting.searchObj.queryData = Object.assign(
    //   this.tableSetting.searchObj.queryData,
    //   query
    // );
    /* 添加监听事件 */
    window.addEventListener("storage", this.updateData);
  },
  beforeDestroy() {
    /* 删除监听事件 */
    window.removeEventListener("storage", this.updateData);
  },
  mounted() {
    this.$refs.tableRef.getTableConfig();
    this.updateData();
  },
  methods: {
    //更新列表页的数据
    updateData() {
      this.$refs.tableRef.searchData();
    },
    // 查询表格数据
    async getTableData(params, sortStr) {
      let queryData = Object.assign({}, params);
      if (sortStr.length > 0) {
        queryData.orders = sortStr;
      }
      if (queryData.estimatedTime) {
        queryData.estimatedStartTime = queryData.estimatedTime[0];
        queryData.estimatedEndTime = queryData.estimatedTime[1];
        delete queryData.estimatedTime;
      }
      if (queryData.createTime) {
        queryData.startCreateTime = queryData.createTime[0];
        queryData.endCreateTime = queryData.createTime[1];
        delete queryData.createTime;
      }
      queryData.statusList = ["8"];
      getListAndPaging(queryData)
        .then((list) => {
          this.$set(this.tableSetting, "tableData", list.data.list);
          this.$set(this.tableSetting, "totalNumber", list.data.total);
        })
        .catch((e) => {
          this.$set(this.tableSetting, "tableData", []);
          this.$set(this.tableSetting, "totalNumber", 0);
        });
    },
    // 跳转至详情页,携带参数分别为：数据id、页面类型、工作票类型
    getDetails(val, type, ticketType) {
      const { href } = this.$router.resolve({
        name: "workTicketProcessingDetails",
        query: {
          val: val,
          pageType: type,
          ticketType: ticketType || "",
        },
      });
      window.open(href, "_blank");
    },
  },
};
</script>
<style lang="less" scoped>
.container {
  /deep/ .el-icon-plus {
    margin-right: 10px;
  }
}
.flow-column {
  display: flex;
  flex-flow: column nowrap;
}
</style>